{
  "properties" : { },
  "id" : "b889c09424704b81b0e9456a455cc411",
  "script" : null,
  "groupId" : "1f1767129e1243fe91e873f62d6954d2",
  "name" : "mongo分页聚合查询实例",
  "createTime" : null,
  "updateTime" : 1648178219770,
  "lock" : null,
  "createBy" : null,
  "updateBy" : "admin",
  "path" : "1112121212",
  "method" : "GET",
  "parameters" : [ ],
  "options" : [ ],
  "requestBody" : "{\r\n    \"data\": {\r\n        \"currentPage\": 1,\r\n        \"pageSize\": 10,\r\n        \"sort\": {\r\n            \"updateTime\": -1,\r\n            \"eventNucleicAcid.collectTime\" : -1\r\n        },\r\n        \"match\": {\r\n            \"dangerType\":\"1\"\r\n        },\r\n        \"like\":{\r\n\r\n        },\r\n        \"date\":{\r\n\r\n        }\r\n    }\r\n}",
  "headers" : [ ],
  "paths" : [ ],
  "responseBody" : null,
  "description" : null,
  "requestBodyDefinition" : {
    "name" : "",
    "value" : "",
    "description" : "",
    "required" : false,
    "dataType" : "Object",
    "type" : null,
    "defaultValue" : null,
    "validateType" : "",
    "error" : "",
    "expression" : "",
    "children" : [ {
      "name" : "data",
      "value" : "",
      "description" : "",
      "required" : false,
      "dataType" : "Object",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ {
        "name" : "currentPage",
        "value" : "1",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "pageSize",
        "value" : "10",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "sort",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "updateTime",
          "value" : "-1",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "eventNucleicAcid.collectTime",
          "value" : "-1",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "match",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "dangerType",
          "value" : "1",
          "description" : "",
          "required" : false,
          "dataType" : "String",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "like",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "date",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      } ]
    } ]
  },
  "responseBodyDefinition" : null
}
================================
import lombok.experimental.var
import lombok.experimental.var
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query
import mongoTemplete;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.collections.CollectionUtils;
import com.cloudwalk.magicapi.utils.DateUtils
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.PageRequest

var condition = body.data


var tableName = "yqfk_jingmen_person_info"


//排序 默认按更新时间倒叙
if (!condition.sort){
    condition.sort = {
        "updateTime":-1
    }
}

//分页 默认第一页每页10条
if (!condition.currentPage && !condition.pageSize){
    condition.currentPage = 1;
    condition.pageSize = 10
}

Pageable pageable = PageRequest.of(condition.currentPage - 1, condition.pageSize);

// var criteria = Criteria.where("checkedFlag").is(1).and("delFlag").is(0).and("eventNucleicAcid.delFlag").is(0)
var criteria = Criteria.where("delFlag").is(0).and("eventNucleicAcid.delFlag").is(0)
var query = new Query(criteria)

var orderList = []
orderList.add(Sort.Order.asc("eventNucleicAcid.collectTime"));

condition.sort.forEach((k, v) -> {
    v == 1 ? orderList.add(Sort.Order.asc(k)) : orderList.add(Sort.Order.desc(k));
});

// var objectList = mongoTemplete.aggregate(Aggregation.newAggregation(
// Aggregation.match(criteria),
// Aggregation.unwind("eventNucleicAcid"),         //平铺事件
// Aggregation.sort(Sort.by(orderList)),           //按事件字段排序
// Aggregation.group("_id").first("name").as("name")       //按_id组装数据
//     .first("cardNo").as("cardNo")                       //取平铺后的第一条数据的值
//     .first("mobile").as("mobile")
//     .first("eventNucleicAcid.samplingCompanyName").as("samplingCompanyName"),  //取平铺后的第一条数据的字段放在主信息
// Aggregation.project("name","cardNo","mobile","samplingCompanyName")     //需要返回的字段（必须在group那一步添加字段）
// ),
// tableName).getMappedResults()

var objectList = mongoTemplete.aggregate(Aggregation.newAggregation(
    // Aggregation.match(criteria),
    Aggregation.unwind("eventNucleicAcid"),
    Aggregation.sort(Sort.by(orderList)),
    Aggregation.group("_id")
        .first("$name").as("name")
        .first("$sex").as("sex")
        .first("$age").as("age")
        .first("$mobile").as("mobile")
        .first("$remark").as("remark")
        .first("$cardNo").as("cardNo")
        .first("$delFlag").as("delFlag")
        .push("$eventNucleicAcid").as("eventNucleicAcid"),
    Aggregation.project("name","sex","age","remark","cardNo","delFlag")
            .and("$eventNucleicAcid").slice(1,-1).as("eventNucleicAcid"),
    Aggregation.match(criteria),
    Aggregation.unwind("eventNucleicAcid"),
    
    Aggregation.skip((condition.currentPage-1)*condition.pageSize),
    Aggregation.limit(condition.pageSize)
),tableName).getMappedResults()

return PageableExecutionUtils.getPage(objectList,pageable,() -> mongoTemplete.count(query.limit(-1).skip(-1), null,tableName));